package com.example.fan.airline_reservation.fragment_class;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
        import android.support.annotation.NonNull;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.fan.airline_reservation.Model.USER;
import com.example.fan.airline_reservation.R;
import com.example.fan.airline_reservation.Traveler_Interface;
import com.example.fan.airline_reservation.traveler_ticketlist;

import java.util.Calendar;
import java.util.Date;

public class traveler_search extends Fragment implements View.OnClickListener, DatePickerDialog.OnDateSetListener{

    EditText from,to;
    TextView dateC;
    Button search;
    int year,month,day;
    Bundle bundle=new Bundle();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_traveler_search
                ,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        from=(EditText)getActivity().findViewById(R.id.fromEditText);

        to=(EditText)getActivity().findViewById(R.id.toEditText);

        search=(Button)getActivity().findViewById(R.id.search_button);
        search.setOnClickListener(this);

        dateC=(TextView)getActivity().findViewById(R.id.dateChoose);
        dateC.setOnClickListener(this);
    }

    /*点击事件*/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_button:
                Traveler_Interface traveler= (Traveler_Interface) getActivity();
                String f=from.getText().toString();
                String t=to.getText().toString();
                Date date=new Date(year-1900,month,day);
                Long Ldate=date.getTime();
                if (f.length()!=0 && t.length()!=0) {
                    bundle = getArguments();
                    USER user = (USER) bundle.getSerializable("USER11");
                    Intent intent = new Intent(getActivity(), traveler_ticketlist.class);
                    intent.putExtra("from", f);
                    intent.putExtra("to", t);
                    intent.putExtra("date", Ldate);
                    intent.putExtra("USER11", user);
                    Log.e("test", Ldate + "");
                    startActivity(intent);
                }else {
                    ToastShow("请输入正确的出发地或目的地");
                }
                break;
            case R.id.dateChoose:
                Calendar calendar=Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                dialog.show();
                break;
        }
    }

    /**/
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String desc = String.format("%d年%d月%d日", year, month + 1, dayOfMonth);
        this.year=year;
        this.month=month;
        this.day=dayOfMonth;
        dateC.setText(desc);
    }

    public void ToastShow(String message){
        Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
    }
}
